
'use strict';
var cmsControllers = angular.module('cmsControllers');

cmsControllers
    .controller('CmsEventCtrl', ['$rootScope', '$scope', '$filter', '$routeParams', 'uploadService',
        'Subscription', 'EventOpt', 'Event', function ($rootScope, $scope, $filter, $routeParams, uploadService, Subscription, EventOpt, Event) {

            $scope.showEventList = function() {
                $scope.eventContent = 'views/component/event/event-list.html';
                var json = EventOpt.opt({action: 'queryEventBySubId', subId: $rootScope.CurrentSub.id},function() {
                    $scope.events = json.data.events;
                });
            }
            $scope.showEventList();

            $scope.createEvent = function() {
                uploadService.maxNumb = 1;
                uploadService.files = [];
                $scope.eventContent = 'views/component/event/event-edit.html';
                $scope.event = {subId: $rootScope.CurrentSub.id};
            };
            $scope.editEvent = function(event) {
                uploadService.maxNumb = 1;
                uploadService.files = [];
                $scope.eventContent = 'views/component/event/event-edit.html';
                $scope.event = event;

                $scope.event.date = $filter('date')($scope.event.date, 'yyyy-MM-dd');
                uploadService.files.push({url: event.image, thumbnailUrl: uploadService.imageUrl(event.image, 'thumbnail')});
                $scope.files = uploadService.files;
            };
            $scope.saveEvent = function() {
                $scope.event.image = uploadService.files[0].url
                new Event($scope.event).$save().then(function() {
                    $scope.showEventList();
                });
            };
            $scope.deleteEvent = function(event) {
                new Event(event).$delete().then(function() {
                    $scope.showEventList();
                });
            };
            $scope.noImage = function() {
                if (uploadService.files.length < 1) {
                    return true;
                } else {
                    return false;
                }
            };
        }]);




